package Z02IOStream.W04CloseStream;

import java.io.*;

public class TestCloseStream {}

class Test{
    public static void main(String[] args) {

        File f = new File("c:/LOL.exe");

        try {
            FileOutputStream fos = new FileOutputStream(f);
            byte[] all = new byte [(int) f.length()];
            for(byte b : all){
                System.out.println(b);
                fos.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }


    }
}

class Test1{
    public static void main(String[] args) {

        File f = new File("c:/LOL.txt");
        FileInputStream fis = null;

        try {
            fis = new FileInputStream(f);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }finally {
            if(null != fis){
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

class Test2{
    File f = new File("c: / LOL.exe");
    FileInputStream fis;

    {
        try {

            // 把流定义在try()里,try,catch或者finally结束的时候，
            // 会自动关闭
            fis = new FileInputStream(f);
            byte[] all = new byte[(int) f.length()];
            fis.read(all);
            for(byte b : all){
                System.out.println(b);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
